package com.ozo.easy.controller;

import com.ozo.easy.common.log.annotation.LogAnno;
import com.ozo.easy.common.model.*;
import com.ozo.easy.pojo.dto.SysFileQueryDTO;
import com.ozo.easy.pojo.vo.SysFileVO;
import com.ozo.easy.service.ISysFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 文件 前端控制器
 *
 * @author ozo
 * @version 1.0
 * @date 2025-02-05 15:44:53
 */
@Tag(name = "文件")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysFile")
public class SysFileController {

    private final ISysFileService sysFileService;

    @Operation(summary = "表格页")
    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sysFile/index");
        return modelAndView;
    }

    @Operation(summary = "添加页")
    @GetMapping("/addForm")
    public ModelAndView addForm() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sysFile/addForm");
        return modelAndView;
    }

    @Operation(summary = "文件上传")
    @PostMapping("/upload")
    public ResultVO<Long> upload(@RequestParam("file") MultipartFile file,
                                 @RequestParam(value = "directory", required = false) String directory) throws Exception {
        return ResultVO.success(sysFileService.upload(file, directory));
    }

    @Operation(summary = "文件下载")
    @GetMapping("/download/{id}")
    public void download(@PathVariable Long id, HttpServletResponse response) throws Exception {
        sysFileService.download(id, response);
    }

    @LogAnno(value = "删除文件", module = "文件")
    @Operation(summary = "删除文件")
    @DeleteMapping("/remove")
    public ResultVO<Void> remove(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        sysFileService.remove(batchDTO);
        return ResultVO.success();
    }

    @Operation(summary = "文件分页")
    @GetMapping("/page")
    public ResultVO<PageResult<SysFileVO>> page(@Valid BasePage basePage, SysFileQueryDTO queryDTO) {
        return ResultVO.success(sysFileService.page(basePage, queryDTO));
    }

}